package com.yisu.expressway.api.web.controller;

import com.google.common.collect.Lists;
import com.yisu.expressway.api.core.service.IGoodsCategoryService;
import com.yisu.expressway.api.domain.request.GoodsCategoryRequest;
import com.yisu.expressway.api.domain.response.GoodsCategoryResponse;
import com.yisu.expressway.api.domain.vo.GoodsCategoryVo;
import com.yisu.expressway.api.web.util.GoodsCategoryUtil;
import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.GoodsCategory;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xlt on 2017/4/28.
 */
@RequestMapping("/goodsCategory")
@Controller
public class GoodsCategoryController extends BaseController {
    @Resource
    private IGoodsCategoryService iGoodsCategoryService;

    /**
     * 获取商品分类列表
     *
     * @param token token令牌， 可为空
     * @return json响应
     */
    @RequestMapping(value = "/getGoodsCategoryList", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult getGoodsCategoryList(@RequestHeader(value = "X-Long-Token", required = false) String token, @RequestBody GoodsCategoryRequest goodsCategoryRequest) {
        JsonResult result = new JsonResult();
        GoodsCategoryResponse goodsCategoryResponse = new GoodsCategoryResponse();
        List<GoodsCategoryVo> goodsCategoryVos = Lists.newArrayList();
        try {
            goodsCategoryVos = funcEncapsulation(GoodsCategoryRequest.EnumForFuncEncapsulation.DATA_ALL.code, goodsCategoryRequest);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        goodsCategoryResponse.setGoodsCategoryVos(goodsCategoryVos);
        result.setData(goodsCategoryResponse);
        return result;
    }

    /**
     * 获取一级分类列表
     *
     * @param token token令牌， 可为空
     * @return json响应
     */
    @RequestMapping(value = "/getFirstLevelCategoryList", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult getFirstLevelCategoryList(@RequestHeader(value = "X-Long-Token", required = false) String token, @RequestBody GoodsCategoryRequest goodsCategoryRequest) {
        JsonResult result = new JsonResult();
        GoodsCategoryResponse goodsCategoryResponse = new GoodsCategoryResponse();
        List<GoodsCategoryVo> goodsCategoryVos = Lists.newArrayList();
        try {
            goodsCategoryVos = funcEncapsulation(GoodsCategoryRequest.EnumForFuncEncapsulation.DATA_FIRST.code, goodsCategoryRequest);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        goodsCategoryResponse.setGoodsCategoryVos(goodsCategoryVos);
        result.setData(goodsCategoryResponse);
        return result;
    }

    /**
     * 通过父级节点获取子集商品分类
     *
     * @param token token令牌， 可为空
     * @return json响应
     */
    @RequestMapping(value = "/getGoodsCategoryListByParentCode", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult getGoodsCategoryListByParentCode(@RequestHeader(value = "X-Long-Token", required = false) String token, @RequestBody GoodsCategoryRequest goodsCategoryRequest) {
        JsonResult result = new JsonResult();
        GoodsCategoryResponse goodsCategoryResponse = new GoodsCategoryResponse();
        List<GoodsCategoryVo> goodsCategoryVos = Lists.newArrayList();
        try {
            goodsCategoryVos = funcEncapsulation(GoodsCategoryRequest.EnumForFuncEncapsulation.DATA_PARENT.code, goodsCategoryRequest);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        goodsCategoryResponse.setGoodsCategoryVos(goodsCategoryVos);
        result.setData(goodsCategoryResponse);
        return result;
    }

        /**
         * 方法封装
         *
         * @param funcType             方法类型 1 获取全部分类列表-后期可做缓存，提高性能
         *                             2 获取一级商品分类
         *                             3 通过父节点ID，获取子集商品分类
         * @param goodsCategoryRequest 请求参数
         * @return
         */
    protected List<GoodsCategoryVo> funcEncapsulation(Integer funcType, GoodsCategoryRequest goodsCategoryRequest) {
        List<GoodsCategoryVo> goodsCategoryVos = Lists.newArrayList();
        switch (funcType) {
            case 1:
                return new GoodsCategoryUtil().switchModel(iGoodsCategoryService.getGoodsCategoryList());
            case 2:
                return new GoodsCategoryUtil().switchModel(iGoodsCategoryService.getFirstLevelCategoryList());
            case 3:
                return new GoodsCategoryUtil().switchModel(iGoodsCategoryService.getGoodsCategoryListByParentCode(goodsCategoryRequest.getParentCode()));
        }
        return goodsCategoryVos;
    }




}
